using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using System;


#if UNITY_EDITOR
using UnityEditor;
#endif

namespace DT.Map.Tools
{
    [ExecuteInEditMode]
    public partial class ToolsHandles : MonoBehaviour
    {
        [Header("回合战报")]
        public RoundDate roundDate;
        [Min(0)] public int CurrentRound = 0;
        [Range(0.0f, 1.0f)] public float OneRoundTime = 0;

        public bool isViewReference = true;

        [Header("基础")]
        public Vector2Int MapSize = new Vector2Int(147, 79);
        // public Vector2Int StartPoint = new Vector2Int(73, 39);
        // public Vector2Int EndPoint = new Vector2Int(75, 39);
        public Color teamcolor;

        [Header("球")]
        [Min(0)] public float Hight = 0;
        public Vector3 BallPoint;

        public List<PlayerFrame> playerFrames = new List<PlayerFrame>();


        public Vector3 m_BallPoint { get; set; }


        Vector2Int MateOffset => MapSize.toHalfXZ();


        void Update()
        {
            OnUpdate();
        }
        void OnUpdate()
        {
            this.transform.hideFlags = HideFlags.NotEditable;
            this.transform.position = Vector3.zero;
            this.transform.rotation = Quaternion.identity;
            this.transform.localScale = Vector3.one;
            foreach (var item in playerFrames)
            {
                item.OnUpdate(OneRoundTime);
            }
            BallUpdate();
        }


        void BallUpdate()
        {
            BallPoint.y = Mathf.Max(0, BallPoint.y);
            m_BallPoint = new Vector3(BallPoint.x + MateOffset.x, BallPoint.y, -BallPoint.z + MateOffset.y).BallPoint(0.2f);
        }

        void PointUpdate(Vector3 point)
        {
            point.x = Mathf.Round(point.x);
            // pos.y = Mathf.Round(pos.y);
            point.z = Mathf.Round(point.z);
        }
    }


    public partial class ToolsHandles
    {

        void OnValidate()
        {
#if UNITY_EDITOR
            OnUpdate();
#endif
        }

        void OnDrawGizmos()
        {
            if (!isViewReference) return;
            foreach (var item in playerFrames)
            {
                DrawPlayer(item);
            }
            DrawFootBall(m_BallPoint);
        }


        void DrawFootBall(Vector3 Point)
        {
            Gizmos.color = new Color(0f, 0, 0f, 1f);
            Gizmos.DrawSphere(Point, 0.24f);
            Gizmos.color = new Color(1f, 1f, 1f, 1f);
            Gizmos.DrawSphere(Point, 0.20f);
#if UNITY_EDITOR
            string name = " 球 ";
            if (!String.IsNullOrEmpty(name))
            {
                float hight = 0.6f;
                Texture2D colorBlock = new Texture2D(64, 64);
                colorBlock.SetPixel(0, 0, new Color(0.5f, 0.5f, 0.5f, 1f));
                colorBlock.Apply();

                // Gizmos.DrawLine(Point, Point + new Vector3(0, hight - 0.05f, 0));
                GUIStyle style = new GUIStyle();
                style.normal.textColor = Color.black;
                style.normal.background = colorBlock;

                float distance = Vector3.Distance(Point, Camera.current.transform.position);

                if (Camera.current.orthographic)
                {
                    distance = Mathf.Lerp(5, 50, Camera.current.orthographicSize);
                }
                float _distance = (distance - 10) / (100 - 10);
                float targetFontSize = Mathf.Lerp(15, 2, _distance);
                style.fontSize = (int)targetFontSize;
                style.alignment = TextAnchor.MiddleCenter;
                style.fontStyle = FontStyle.Bold;
                Handles.Label(Point + new Vector3(0, hight, 0), name, style);
            }
#endif
        }
        void DrawPlayer(PlayerFrame playerFrame)
        {
            Vector3 p = playerFrame.GetWorldPosition(playerFrame.CurrentPos);
            DrawPlayer(p, playerFrame.team == TeamName.ATeam ? new Color(0.1783019f, 0.3156173f, 1f, 1f) : Color.red, playerFrame.isHolding, playerFrame.PlayerName);
        }
        void DrawPlayer(Vector3 _Point, Color _color, bool isFrist, string name)
        {
            float playerSize = 0.5f;
            Vector3 Point = _Point + new Vector3(0, 0.9f, 0);
            Gizmos.color = new Color(0f, 0, 0f, 1f);
            Gizmos.DrawCube(Point, new Vector3(playerSize * 1.2f, 1.9f, playerSize * 1.2f));
            if (isFrist)
                Gizmos.DrawSphere(Point + new Vector3(0, 1.2f, 0), playerSize * 1.2f * 0.5f);

            Gizmos.color = _color;
            if (isFrist)
                Gizmos.DrawSphere(Point + new Vector3(0, 1.2f, 0), playerSize * 0.5f);
            Gizmos.DrawCube(Point, new Vector3(playerSize, 1.8f, playerSize));

#if UNITY_EDITOR
            if (!String.IsNullOrEmpty(name))
            {
                float hight = 1.8f;
                Texture2D colorBlock = new Texture2D(64, 64);
                colorBlock.SetPixel(0, 0, _color);
                colorBlock.Apply();

                // Gizmos.DrawLine(Point, Point + new Vector3(0, hight - 0.05f, 0));
                GUIStyle style = new GUIStyle();
                style.normal.textColor = Color.black;
                style.normal.background = colorBlock;

                float distance = Vector3.Distance(Point, Camera.current.transform.position);

                if (Camera.current.orthographic)
                {
                    distance = Mathf.Lerp(5, 50, Camera.current.orthographicSize);
                }
                float _distance = (distance - 10) / (100 - 10);
                float targetFontSize = Mathf.Lerp(15, 2, _distance);
                style.fontSize = (int)targetFontSize;
                style.alignment = TextAnchor.MiddleCenter;
                style.fontStyle = FontStyle.Bold;
                Handles.Label(Point + new Vector3(0, hight, 0), name, style);
            }
#endif
        }



        void DrawRay(Vector3 startPoint, Vector3 endPoint)
        {
            Vector3 v = endPoint - startPoint;
            if (v.magnitude < 1) return;

            Gizmos.color = new Color(1f, 1f, 1f, 1f);    //   #FFFFFF
            Gizmos.DrawLine(startPoint + new Vector3(0, 0.1f, 0), endPoint + new Vector3(0, 0.1f, 0));

#if UNITY_EDITOR
            // Handles.PositionHandle(startPoint,  Quaternion.LookRotation(v));
            // Handles.Slider(endPoint, v, 0.2f, Handles.ConeHandleCap, 1);
            Handles.ConeHandleCap(0, endPoint + new Vector3(0, 0.1f, 0), Quaternion.LookRotation(v), 0.2f, EventType.Repaint);


#endif
        }
    }



#if UNITY_EDITOR

    [CustomEditor(typeof(ToolsHandles))]
    public class ToolsHandlesEditor : Editor
    {
        ToolsHandles m_target;
        ToolsHandles tag
        {
            get
            {
                if (m_target == null)
                {
                    m_target = target as ToolsHandles;
                }
                return m_target;
            }
        }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

        }
        private void OnSceneGUI()
        {

            if (tag == null) return;
            // if (tag.StartPointHandle)
            // {
            //     tag.StartPoint = Handles.PositionHandle(tag.m_startPoint, Quaternion.identity).toV2Int();

            // }
            // if (tag.EndPointHandle)
            // {
            //     Vector3 pp = Handles.PositionHandle(tag.m_endPoint, Quaternion.identity);
            //     tag.EndPoint =  pp.toV2Int();


            // }

        }
    }


    public static class Extensions
    {
        public static Vector2Int toV2Int(this Vector3 point)
        {
            return new Vector2Int((int)point.x, (int)point.z);
        }
        public static Vector3 toV3(this Vector2Int point)
        {
            return new Vector3(point.x, 0, point.y);
        }
        public static Vector2Int toHalfXZ(this Vector2Int point)
        {
            return new Vector2Int(-(int)(point.x * 0.5f), (int)(point.y * 0.5f));
        }

        public static Vector3 BallPoint(this Vector3 point, float offset)
        {
            float off = Mathf.Lerp(offset, 0, Mathf.Clamp(point.y, 0, offset));
            return new Vector3(point.x, point.y + off, point.z);
        }
    }
#endif
}
